package com.ruoyi.monitor.utils.Msg;


import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.SendBatchSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.tea.TeaException;
import com.aliyun.teaopenapi.models.Config;
import com.aliyun.teautil.Common;
import com.aliyun.teautil.models.RuntimeOptions;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.monitor.domain.SysMsg;
import com.ruoyi.monitor.utils.Common.impl.CommonSender;
import com.ruoyi.monitor.utils.Email.Mail;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class SMSCenter extends CommonSender {
// This file is auto-generated, don't edit it. Thanks.
        /**
         * 使用AK&SK初始化账号Client
         * @param accessKeyId
         * @param accessKeySecret
         * @return Client
         * @throws Exception
         */
        public static Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
            Config config = new Config()
                    // 必填，您的 AccessKey ID
                    .setAccessKeyId(accessKeyId)
                    // 必填，您的 AccessKey Secret
                    .setAccessKeySecret(accessKeySecret);
            // 访问的域名
            config.endpoint = "dysmsapi.aliyuncs.com";
            return new Client(config);
        }

        public static SysMsg sendTo(SysUser user)  {
            // 工程代码泄露可能会导致AccessKey泄露，并威胁账号下所有资源的安全性。以下代码示例仅供参考，建议使用更安全的 STS 方式，更多鉴权访问方式请参见：https://help.aliyun.com/document_detail/378657.html
            SysMsg message = new SysMsg();
            Client client = null;
            try {
                client = SMSCenter.createClient(Message.AccessKeyId, Message.AccessKeySecret);
            } catch (Exception e) {
                e.printStackTrace();
            }
            SendSmsRequest sendSmsRequest = new SendSmsRequest()
                    .setSignName(Message.SignName)
                    .setTemplateCode(Message.TemplateCode)
                    .setPhoneNumbers(user.getPhonenumber())
                    .setTemplateParam(Message.TemplateParam);
            RuntimeOptions runtime = new RuntimeOptions();
            Integer statusCode = null;
            try {
                // 复制代码运行请自行打印 API 的返回值
                SendSmsResponse sendSmsResponse = client.sendSmsWithOptions(sendSmsRequest, runtime);
                System.out.println(sendSmsResponse.statusCode);
                statusCode = sendSmsResponse.statusCode;
                System.out.println(Message.SUCCESS.equals(statusCode));

            } catch (TeaException error) {
                // 如有需要，请打印 error
                Common.assertAsString(error.message);
            } catch (Exception _error) {
                TeaException error = new TeaException(_error.getMessage(), _error);
                // 如有需要，请打印 error
                Common.assertAsString(error.message);
            }
            if(Message.SUCCESS.equals(statusCode)){
                message.setContent(Message.TemplateParam);
                message.setDate(new Date());
                message.setRank(Mail.MODEL_TIPS);
                message.setTitle(Mail.HEAD);
                message.setType(Message.TYPE_MESSAGE);
                System.out.println("短信发送成功！");
                return message;
            }else {
                return null;
            }
        }



    public static Boolean sendBatch(){
            // 工程代码泄露可能会导致AccessKey泄露，并威胁账号下所有资源的安全性。以下代码示例仅供参考，建议使用更安全的 STS 方式，更多鉴权访问方式请参见：https://help.aliyun.com/document_detail/378657.html
            Client client = null;
            try {
                client = SMSCenter.createClient(Message.AccessKeyId, Message.AccessKeySecret);
            } catch (Exception e) {
                e.printStackTrace();
            }
            SendBatchSmsRequest sendBatchSmsRequest = new SendBatchSmsRequest();
            RuntimeOptions runtime = new RuntimeOptions();
            try {
                // 复制代码运行请自行打印 API 的返回值
                client.sendBatchSmsWithOptions(sendBatchSmsRequest, runtime);
            } catch (TeaException error) {
                // 如有需要，请打印 error
                Common.assertAsString(error.message);
            } catch (Exception _error) {
                TeaException error = new TeaException(_error.getMessage(), _error);
                // 如有需要，请打印 error
                Common.assertAsString(error.message);
            }
            return true;

        }





    }



//    public static void main(String[] args) {
//        // 工程代码泄露可能会导致AccessKey泄露，并威胁账号下所有资源的安全性。以下代码示例仅供参考，建议使用更安全的 STS 方式，更多鉴权访问方式请参见：https://help.aliyun.com/document_detail/378657.html
//        Client client = null;
//        try {
//            client = Sample.createClient(Message.AccessKeyId, Message.AccessKeySecret);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        SendSmsRequest sendSmsRequest = new SendSmsRequest()
//                .setSignName("阿里云短信测试")
//                .setTemplateCode("SMS_154950909")
//                .setPhoneNumbers("15906155807")
//                .setTemplateParam("{\"code\":\"1234\"}");
//        RuntimeOptions runtime = new RuntimeOptions();
//        try {
//            // 复制代码运行请自行打印 API 的返回值
//            SendSmsResponse sendSmsResponse = client.sendSmsWithOptions(sendSmsRequest, runtime);
//
//            System.out.println(sendSmsResponse.statusCode);
//        } catch (TeaException error) {
//            // 如有需要，请打印 error
//            Common.assertAsString(error.message);
//        } catch (Exception _error) {
//            TeaException error = new TeaException(_error.getMessage(), _error);
//            // 如有需要，请打印 error
//            Common.assertAsString(error.message);
//        }
//    }
